package com.baturu.vin.honda.script;

import com.baturu.common.guava2.Lists2;
import com.baturu.vin.dto.EPCPartsDTO;
import com.baturu.vin.honda.dal.dao.SimpleDAO;
import com.baturu.vin.honda.dal.entity.Vin;
import com.baturu.vin.kit.StreamKit;
import com.baturu.vin.test.AbstractBaseCase;
import com.google.common.base.Joiner;
import com.google.common.collect.Sets;
import org.apache.commons.lang.StringUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author: chenjiahao
 * @Time: 2019/12/16
 */
public class LiYangVin extends AbstractBaseCase {

    @Autowired
    private SimpleDAO simpleDAO;

    @Test
    public void name() {
        int ma = simpleDAO.querytmp_1216_3MAXId();
        int s = 0;
        int e = 10000;
        while(s<ma){
            deal(s, e);
            s+=10000;
            e+=10000;
        }
        deal(s, ma);
    }

    private void deal(int s, int e) {
        List<Vin> vins = simpleDAO.querytmp_1216_3(s, e);
        StringBuilder pre= new StringBuilder("INSERT into tmp_1216_4 (btrid,vincode,btrpartsName,partscode ) values ");
        Map<String, List<Vin>> collect = vins.stream().collect(Collectors.groupingBy(Vin::getVinCode));
        for (List<Vin> value : collect.values()) {
            String btrId = value.get(0).getBtrId();
            Set<String> btrparts = value.stream().map(Vin::getBtrPartsName).filter(StringUtils::isNotBlank).collect(Collectors.toSet());
            String vinCode = value.get(0).getVinCode();
            List<EPCPartsDTO> epcPartsDTOS = StreamKit.partitionStream(btrparts,30)
                    .flatMap(l->epcQueryService.queryEPCPartsByBtrPartsName(vinCode,
                            Sets.newHashSet(l)).stream()).collect(Collectors.toList());
            Map<String, List<EPCPartsDTO>> collect1 = epcPartsDTOS.stream().collect(Collectors.groupingBy(EPCPartsDTO::getBtrPartsName));
            for (List<EPCPartsDTO> partsDTOS : collect1.values()) {
                String btrPartsName = partsDTOS.get(0).getBtrPartsName();
                String join = Joiner.on(",").join(Lists2.transform(partsDTOS, EPCPartsDTO::getPartsCode));
                pre.append("('").append(btrId).append("','").append(vinCode).append("','").append(btrPartsName).append("','").append(join).append("'),");
            }
        }
//        for (Vin d : vins) {
//            List<EPCPartsDTO> epcPartsDTOS = epcQueryService.queryEPCPartsByPartsCode(d.getVinCode(), Sets.newHashSet(d.getBtrPartsName()));
//            String join = Joiner.on(",").join(Lists2.transform(epcPartsDTOS, EPCPartsDTO::getPartsCode));
//            pre.append("('").append(d.getBtrId()).append("','").append(d.getVinCode()).append("','").append(d.getBtrPartsName()).append("','").append(join).append("'),");
//        }
        pre.deleteCharAt(pre.lastIndexOf(","));
        simpleDAO.insert(pre.toString());
    }

}
